package cate.game.role.show.msg;

import cate.common.annotation.OperationSetter;
import cate.common.table.d.GDOperation;
import cate.common.table.show.heroskin.row.HeroSkinLevelRow;
import cate.common.util.GameResult;
import cate.game.GameBody;
import cate.game.client.ClientContext;
import cate.game.client.msg.ClientMsg;
import cate.game.client.msg.MsgConfig;
import cate.game.client.msg.MsgInput;
import cate.game.client.msg.id.MsgIdBBlog;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import cate.game.role.show.heroskin.HeroSkin;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

@ClientMsg(title = "英雄皮肤升级 の 请求")
@OperationSetter(GDOperation.HERO_SKIN_LEVEL_UP)
public class HeroSkinLevelUpReq implements MsgInput {
    @NoteField("皮肤tid")
    public int tid;

    @Override
    public void execute(GameBody game, ClientContext cc) {
        GameResult<Void> r = settle(cc.getRole());
        game.notice.message(cc.getRole(), r,true);
    }

    private GameResult<Void> settle(Role role) {
        GameResult<Void> r = new GameResult<>();
        HeroSkin item = role.getShow().heroSkin.getItem(tid);
        if (item == null) {
            return r.fail("皮肤不存在");
        }
        HeroSkinLevelRow row = item.getLevelRow(role.getGame());
        if (row == null) {
            return r.fail("配置不存在");
        }
        if (StringUtils.isBlank(row.consumeStr)) {
            return r.fail("配置不存在");
        }
        HeroSkinLevelRow nextRow = item.getNextLevelRow(role.getGame());
        if (nextRow == null) {
            return r.fail("配置不存在");
        }
        MixRes consume = new MixRes(row.consumeStr);
        GameResult<MixResItem> consumeRes = consume.consume(role);
        if (!consumeRes.ok()) {
            return r.fail(consumeRes);
        }
        item.level++;
        role.sendNow(new HeroSkinLevelUpResp(item));
        role.getGame().notice.message(role, "升级成功");
        role.getBase().updatePower();
        return r.success();
    }


    @Override
    public MsgConfig conf() {
        return MsgIdBBlog.HERO_SKIN_LEVEL_UP_REQ;
    }
}
